<?php


namespace app\common\model;

class PushRecord extends Base
{
    protected $autoWriteTimestamp = true;

    /**
     * 添加分发规则 生成每日记录
     * @param $push array 分发规则数组
     */
    public function addPushToRecord($push)
    {
        // 判断是否删除规则
        /*if (!empty($push['delete_time'])) {
            return true;
        }*/
        // 判断线索接受数量
        if ($push['up_limit'] <= 0) {
            return true;
        }
        // 判断是否启用
        if ($push['status'] != 1) {
            return true;
        }
        // 判断线索接收日期
        $date = date('Y-m-d H:i:s');
        if ($date < $push['start_time'] || $date > $push['end_time']) { // 不在时间段内，不存记录
            return true;
        }
        $merch = \db('merchant')->where('delete_time',"null")->where('id',$push['mer_id'])->find();
        // 判断商户是否开启
        if (empty($merch) || $merch['status'] != 1) {
            return true;
        }
        // 判断商户时候余额大于0
        if ($merch['balance'] <= 0) {
            return true;
        }
        // 判断接收星期几
        if (!empty($push['week_time'])) {
            $weekArr = json_decode($push['week_time'], true);
            //全部星期
            if (empty($weekArr) || isset($weekArr[10])) {
                $this->insertPushRecord($push); // 再时间段内，添加分发规则到记录中
                return true;
            }

            $nowWeek = date('w');
            if (isset($weekArr[$nowWeek])) {
                $this->insertPushRecord($push); // 再时间段内，添加分发规则到记录中
                return true;
            }
        } else {
            empty($this->getPushRecord($push)) && $this->insertPushRecord($push);
        }
        return true;
    }

    /**
     * 编辑分发规则 生成每日记录
     * @param $push array 分发规则数组
     */
    public function editPushToRecord($push)
    {
        // 判断是否删除规则
        if (!empty($push['delete_time'])) {
            return true;
        }
        // 判断线索接受数量
        if ($push['up_limit'] <= 0) {
            return true;
        }
        // 判断是否启用
        if ($push['status'] != 1) {
            return true;
        }
        // 判断线索接收日期
        $date = date('Y-m-d H:i:s');
        if ($date < $push['start_time'] || $date > $push['end_time']) { // 不在时间段内，不存记录
            return true;
        }
        $merch = \db('merchant')->where('delete_time',"null")->where('id',$push['mer_id'])->find();
        // 判断商户是否开启
        if (empty($merch) || $merch['status'] != 1) {
            return true;
        }
        // 判断商户时候余额大于0
        if ($merch['balance'] <= 0) {
            return true;
        }
        // 判断接收星期几
        if (!empty($push['week_time'])) {
            $weekArr = json_decode($push['week_time'], true);
            //全部星期
            if (empty($weekArr) || isset($weekArr[10])) {
                empty($this->getPushRecord($push)) && $this->insertPushRecord($push); // 不存在才生成记录
                return true;
            }

            $nowWeek = date('w');
            if (isset($weekArr[$nowWeek])) {
                empty($this->getPushRecord($push)) && $this->insertPushRecord($push);
                return true;
            }
        } else {
            empty($this->getPushRecord($push)) && $this->insertPushRecord($push);
        }

        return true;
    }

    /*
     * 商户状态 启用状态时
     * 商户余额改动 增加时
     * 判断是否需要生成分发规则生效记录
     */
    public function addByMerchs($merch_ids)
    {
        if (empty($merch_ids) || !is_array($merch_ids)) {
            return false;
        }

        $get_merch_ids = \db('merchant')
            ->where('delete_time',"null")
            ->where('balance', ">", 0)
            ->where('status', 1)
            ->whereIn('id',$merch_ids)
            ->column('id');

        $pushs = \db('push')
            ->where('delete_time',"null")
            ->whereIn('mer_id', $get_merch_ids)
            ->where('up_limit', ">", 0)
            ->where('status', 1)
            ->select();
        foreach ($pushs as $push) {
            // 判断线索接收日期
            $date = date('Y-m-d H:i:s');
            if ($date < $push['start_time'] || $date > $push['end_time']) { // 不在时间段内，不存记录
                continue;
            }
            // 判断接收星期几
            if (!empty($push['week_time'])) {
                $weekArr = json_decode($push['week_time'], true);
                //全部星期
                if (empty($weekArr) || isset($weekArr[10])) {
                    empty($this->getPushRecord($push)) && $this->insertPushRecord($push); // 不存在才生成记录
                    continue;
                }

                $nowWeek = date('w');
                if (isset($weekArr[$nowWeek])) {
                    empty($this->getPushRecord($push)) && $this->insertPushRecord($push);
                    continue;
                }
            } else {
                empty($this->getPushRecord($push)) && $this->insertPushRecord($push);
            }
        }
        return true;
    }

    public function getPushRecord($push) {
        $record = \db('push_record')
            ->where('create_date', date('Y-m-d'))
            ->where('push_id', $push['id'])
            ->where('city', $push['city'])
            ->where('mer_id', $push['mer_id'])
            ->where('pro_id', $push['pro_id'])
            ->find();
        if (!empty($record) && $push['up_limit'] > $record['up_limit']) {
            \db('push_record')->where('id', $record['id'])->update(['up_limit' => $push['up_limit']]);
        }
        return $record;
    }
    public function insertPushRecord($push)
    {
        $record = [
            'update_time' => time(),
            'create_date' => date('Y-m-d'),
            'push_id' => $push['id'],
            'pro_id' => $push['pro_id'],
            'mer_id' => $push['mer_id'],
            'city' => $push['city'],
            'up_limit' => $push['up_limit'],
            'start_time' => $push['start_time'],
            'end_time' => $push['end_time'],
            'timing_time' => $push['timing_time'],
            'week_time' => $push['week_time'],
            'week_day' => $push['week_day']
        ];
        \db('push_record')->insert($record);
    }
}